package com.qyt.hotel.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qyt.hotel.entity.Result;
import com.qyt.hotel.entity.Room;
import com.qyt.hotel.entity.RoomState;
import com.qyt.hotel.entity.StatusCode;
import com.qyt.hotel.service.RoomService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/room")
public class RoomController {

    @Resource
    private RoomService roomService;

    @GetMapping("/list")
    public Result getRoomList(@RequestParam("pageIndex") Integer pageIndex, @RequestParam("pageSize") Integer pageSize, @RequestParam("roomTypeId") Integer roomTypeId, @RequestParam("roomStateId") Integer roomStateId) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Room> roomList = roomService.getRoomList(roomTypeId, roomStateId);
        // 封装查出的信息,并设置连续显示的页数
        PageInfo pageInfo = new PageInfo(roomList, 5);
        return new Result("查询成功", true, StatusCode.OK, pageInfo);
    }

    @GetMapping("/getOne")
    public Result getOne(@RequestParam("roomId") String roomId) {
        Room room = roomService.getOne(roomId);
        System.out.println(room);
        if (null != room) {
            return new Result("查询成功", true, StatusCode.OK, room);
        } else {
            return new Result("查询失败", false, StatusCode.ERROR, null);
        }
    }

    @PostMapping("/add")
    public Result addRoom(Room room) {
        String roomId = roomService.getRoomId(room.getRoomId());
        if (null == roomId) {
            Integer count = roomService.addRoom(room);
            if (null != count) {
                return new Result("添加成功", true, StatusCode.OK, null);
            } else {
                return new Result("添加失败", true, StatusCode.OK, null);
            }
        } else {
            return new Result("房间号重复", false, StatusCode.ERROR, null);
        }
    }

    @PostMapping("/update")
    public Result updateRoom(@RequestParam("id") String id, @RequestParam("roomId") String roomId, @RequestParam("roomStateId") Integer roomStateId, @RequestParam("roomTypeId") Integer roomTypeId, @RequestParam("roomDescription") String roomDescription) {
        // 表示房间号未修改,也是可以修改成功的
        if (id.equals(roomId)) {
            Integer count = roomService.updateRoom(id, roomId, roomStateId, roomTypeId, roomDescription);
            return new Result("修改成功", true, StatusCode.OK, null);
        } else {
            String roomId2 = roomService.getRoomId(roomId);
            if (roomId2 == null) {
                Integer count = roomService.updateRoom(id, roomId, roomStateId, roomTypeId, roomDescription);
                return new Result("修改成功", true, StatusCode.OK, null);
            } else {
                return new Result("房间名重复", false, StatusCode.ERROR, null);
            }
        }
    }

    @PostMapping("/delete")
    public Result deleteRoom(@RequestParam("roomId") String roomId) {
        Room room = roomService.getOne(roomId);
        RoomState roomState = room.getRoomState();
        // 已入住的房间不能删除
        if ("已入住".equals(roomState.getRoomStateName())) {
            return new Result("此房间已入住", false, StatusCode.ERROR, null);
        } else {
            Integer count = roomService.deleteRoom(roomId);
            if (null != count) {
                return new Result("删除成功", true, StatusCode.OK, null);
            } else {
                return new Result("删除失败", false, StatusCode.ERROR, null);
            }
        }
    }
}
